from db_connect import ConnMysqlSlave
from db_connect import ConnMysql
import aiomysql


class Product():
    
    def __init__(self, product_name, product_price, product_image):

        self.product_name = product_name
        self.product_price = product_price
        self.product_image = product_image

    async def result(self):

        
        async with ConnMysqlSlave().pool.acquire() as c:

            await c.ping()

            async with c.cursor(aiomysql.DictCursor) as cur:

                select_sql = 'select * from product where product_name = %s'

                await cur.execute(select_sql,(self.product_name,))

                product_name = await cur.fetchone()


        if product_name:

            return {
                        'status': -1,
                        'errorCode': 3,
                        'errorMsg': "该商品已经有了",
                        'resultBody': None,
                    }
            
        async with ConnMysql().pool.acquire() as c:

            async with c.cursor(aiomysql.DictCursor) as cur:

                sql = "INSERT INTO product (product_name,product_image,product_price) VALUES (%s,%s,%s)"

                await cur.execute(sql,(self.product_name,self.product_image,self.product_price))

                await c.commit()

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': 'OK',
        }
            

      